package com.sd.view;

import java.util.ArrayList;
import java.util.List;
import java.util.Vector;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AbsListView;
import android.widget.BaseExpandableListAdapter;
import android.widget.ImageButton;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.LinearLayout.LayoutParams;

import com.sd.activity.R;
import com.sd.database.ScanItem;

/**
 * 
 * @author Alex.Lu
 *
 */
public class WapMenuAdapter extends BaseExpandableListAdapter{

	private List<MenuItem> mList;
	private List<MenuItem> mHistory;
	private LayoutInflater mInflater;
	
	
	public WapMenuAdapter(Context context,Vector<ScanItem> scans){
		mInflater = LayoutInflater.from(context);
		
		mList = new ArrayList<MenuItem>();
		MenuItem item = new MenuItem();
		item.tag = 0;
		
		mHistory= new ArrayList<MenuItem>();
		for(int i=0;i<scans.size();i++){
			ScanItem scan = scans.get(i);
			MenuItem h = new MenuItem();
			h.tag = 0;
			h.name = scan.name;
			h.url = scan.url;
			mHistory.add(h);
		}
		
		//首页
		item.list = mHistory;
		mList.add(item);
		MenuItem item1 = new MenuItem();
		item1.tag = 1;
		item1.url = "http://wap.qidian.cn/wap2/index.do?";
		mList.add(item1);
		
		//排行
		MenuItem item2 = new MenuItem();
		item2.tag = 2;
		item2.url = "http://wap.qidian.cn/wap2/top/gettoplist.do?mtd=gettoplist";
		mList.add(item2);
		
		//书籍分类
		MenuItem item3 = new MenuItem();
		item3.tag = 3;
		item3.url = "http://wap.qidian.cn/wap2/index.do?mtd=flIndex&wts=booktotal&type=7";
		mList.add(item3);
		
		MenuItem item4 = new MenuItem();
		item4.tag = 4;
		item4.url = null;
		mList.add(item4);
	}
	
	public void addHistory(ScanItem scan){
		MenuItem h = new MenuItem();
		h.tag = 0;
		h.name = scan.name;
		h.url = scan.url;
		mHistory.add(0, h);
		if(mHistory.size()>10){
			mHistory.remove(mHistory.size()-1);
		}
	}
	
	@Override
	public boolean areAllItemsEnabled() {
		// TODO Auto-generated method stub
		return false;
	}

	// -----------------Child----------------//
	@Override
	public Object getChild(int groupPosition, int childPosition) {
		if(mList.get(groupPosition).list!=null){
			return mList.get(groupPosition).list.get(childPosition);
		}
		return null;
	}

	@Override
	public long getChildId(int groupPosition, int childPosition) {
		return childPosition;
	}

	@Override
	public int getChildrenCount(int groupPosition) {
		if(mList.get(groupPosition).list != null){
			return mList.get(groupPosition).list.size();
		}
		return 0;
	}

	@Override
	public View getChildView(int groupPosition, int childPosition,
			boolean isLastChild, View convertView, ViewGroup parent) {
		TextView name = null;
		if(convertView==null){
			convertView = (LinearLayout)mInflater.inflate(R.layout.menu_child_view, null);
			name  = (TextView) convertView.findViewById(R.id.name);
			convertView.setTag(name);
		}else{
			name = (TextView)convertView.getTag();
		}
		String string = mList.get(groupPosition).list.get(childPosition).name;
		name.setText(string);
		name.setLayoutParams(new LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.WRAP_CONTENT));
		convertView.setLayoutParams(new AbsListView.LayoutParams(AbsListView.LayoutParams.FILL_PARENT, AbsListView.LayoutParams.WRAP_CONTENT));
		return convertView;
	}


	@Override
	public long getCombinedChildId(long groupId, long childId) {
		// TODO Auto-generated method stub
		return 0;
	}

	@Override
	public long getCombinedGroupId(long groupId) {
		// TODO Auto-generated method stub
		return 0;
	}

	@Override
	public Object getGroup(int groupPosition) {
		// TODO Auto-generated method stub
		return mList.get(groupPosition);
	}

	@Override
	public int getGroupCount() {
		// TODO Auto-generated method stub
		return mList.size();
	}

	@Override
	public long getGroupId(int groupPosition) {
		// TODO Auto-generated method stub
		return groupPosition;
	}

	@Override
	public View getGroupView(int groupPosition, boolean isExpanded,
			View convertView, ViewGroup parent) {
		int tag = mList.get(groupPosition).tag;
		View view = mInflater.inflate(R.layout.menu_parent_view, null);
		ImageButton groupname = (ImageButton) view.findViewById(R.id.name);
		groupname.setClickable(false);
		switch (tag) {
		case 0:
			groupname.setImageResource(R.drawable.wap_menu_history_);
			break;
		case 1:
			groupname.setImageResource(R.drawable.wap_menu_index_);
			break;
		case 2:
			groupname.setImageResource(R.drawable.wap_menu_ranking_);
			break;
		case 3:
			groupname.setImageResource(R.drawable.wap_menu_classify_);
			break;
		case 4:
			groupname.setImageResource(R.drawable.wap_menu_userchange_);
			break;
		default:
			break;
		}
		groupname.setBackgroundDrawable(null);
		return view;
	}

	@Override
	public boolean hasStableIds() {
		// TODO Auto-generated method stub
		return false;
	}

	@Override
	public boolean isChildSelectable(int groupPosition, int childPosition) {
		// TODO Auto-generated method stub
		return true;
	}
	
	public class MenuItem{
		public int tag;
		public String name;
		public String url;
		public List<MenuItem> list;
	}

}
